package com.asset.dispoal.business.repayment.service.command;

import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.business.repayment.domain.db.mapper.CaseRepaymentLogsMapper;
import com.asset.dispoal.business.repayment.dto.RepaymentInfoResponse;
import com.asset.dispoal.business.repayment.service.factory.IRepaymentFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

/**
 * @description: 工厂 + 策略， 生产出不同状态的案件还款详情
 * @author: yf
 * @date: 2022-09-26 16:30:17
 */
@Slf4j
@Repository
public class GetRepaymentInfoBycaseIdCommand implements BaseCommand<String, Result> {
    @Autowired
    private BsCaseMapper caseMapper;
    @Autowired
    private IRepaymentFactory repaymentFactory;
    @Autowired
    private CaseRepaymentLogsMapper caseRepaymentLogsMapper;

    @Override
    public Result<RepaymentInfoResponse> execute(String caseId) {
        BsCase bsCase = caseMapper.queryById(caseId);
        //判断当前案件是否还清
        if (StringUtil.isNotEmptyNull(bsCase.getCaseIsPay()) && ("1".equals(bsCase.getCaseIsPay()))) {
            return new Result<>(498, "当前案件已无需还款！");
        }
        //查询当前案件是否存在正在审核的还款项
        int approvalNums = caseRepaymentLogsMapper.getTodoReviewRepayment(caseId);
        if (approvalNums > 0) {
            return new Result<>(499, "当前案件有正在还款审核项！");
        }
        log.debug("案件当前的状态为: {}", bsCase.getCaseState());
        //否则走调解或者和解状态逻辑
        return Result.ok(repaymentFactory.createRepaymentInfo(bsCase));
    }
}
